<?php
require_once __DIR__ . '/php_lib/Request.php';


class BaiduTop {
  const URL = 'https://top.baidu.com/board';
  static function getCards() {

    $result = Request::get_mobile(self::URL, [
      'tab' => 'realtime'
    ]);

    $keyStr = '<!--s-data:';
    $a = strpos($result, $keyStr) + strlen($keyStr);

    $data = null;
    $cards = null;
    if ($a >= 0) {
      $b = strpos($result, '-->', $a);
      if ($b >= 0) {
        $dataJSON = substr($result, $a, $b - $a);
        $data = json_decode($dataJSON, true);
      }
    }
    if (!is_null($data)) {
      $success  = $data['success'];
      if ($success) {
        $cards = $data['cards'];
      }
    }
    return $cards;
  }
}
